package com.yoomiito.app.widget.dialog

import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.order.OrderingInfo
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.SpannableUtils
import kotlinx.android.synthetic.main.item_pay.*
import kotlinx.android.synthetic.main.dialog_pay.*

import java.util.concurrent.TimeUnit

class TwoTypesOfPayDialog : BaseDialog {
    private val OTHER_APY = 1
    private val MONEY_PAY = 2
    private lateinit var mOrderPayMoney: String//待支付款
    private lateinit var mUserAccountBalance: String//用户账户余额
    private lateinit var mUserOtherBalance: String//用于第三方账户余额
    var payTypeChangeListener: (Int) -> Unit = {}

    constructor(context: Context) : super(context, R.style.dialog_2) {}

    constructor(context: Context, themeResId: Int) : super(context, R.style.dialog_2) {
    }

    protected constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?) : super(context, cancelable, cancelListener) {}

    override fun setView(): View {
        setWidthPercent(1.0)
        setmHeightPercent(0.65)
        setmGravity(Gravity.BOTTOM)
        return LayoutInflater.from(mContext).inflate(R.layout.dialog_pay, null, false)
    }

    override fun initView() {
        firstTypesPayLl.visibility = View.VISIBLE
        wx_pay_ll.visibility = View.GONE
        aLiPayLl.visibility = View.GONE
        updateState()

        close.setOnClickListener {
            dismiss()
//            setResult(true)
        }
        pay.setOnClickListener {
            payTypeChangeListener(
                    when {
                        firstTypesPaySv.isSelected and item_pay_money.isSelected -> 3
                        firstTypesPaySv.isSelected -> 1
                        item_pay_money.isSelected -> 2
                        else ->{
                            -1
                        }
                    })
        }

        firstTypesPaySv.setChangeListener { _, _ ->
            //电子币
            changeWay(OTHER_APY)
        }

        item_pay_money.setChangeListener { _, _ ->
            //余额
            changeWay(MONEY_PAY)
        }

    }

    fun setMoney(orderingInfo: OrderingInfo) {

        this.mOrderPayMoney = orderingInfo.orderInfo.amount_paid
        this.mUserAccountBalance = orderingInfo.moneyInfo.amount
        this.mUserOtherBalance = orderingInfo.moneyInfo.current

        updateState()
    }

    private fun changeWay(payType: Int) {
        //可点击的情况下 才可以更改状态
        if (item_pay_money.isEnabled)item_pay_money.setSelect(MONEY_PAY == payType)
        if (firstTypesPayTitleTv.isEnabled) firstTypesPaySv.setSelect(OTHER_APY == payType)
    }

    private fun updateState() {
        if (pay_money != null) {
            updateData()

            val isOtherPay = AllUtils.moneyCompare(mUserOtherBalance, mOrderPayMoney)
            val isAccountPay = AllUtils.moneyCompare(mUserAccountBalance, mOrderPayMoney)
            pay.isEnabled = true
            val drawable = pay.background as GradientDrawable
            drawable.setColor(ResUtil.getColor(R.color.color_orange))
            when {
                isOtherPay and  isAccountPay -> {//都可以支付 2选1
                    firstTypesPaySv.setSelect(true)
                    item_pay_money.setSelect(false)
                }
                isOtherPay -> { //其他方式支付
                    firstTypesPaySv.setSelect(true)

                    item_pay_money.setSelect(false)
                    item_pay_money.setUnClickSelect(R.drawable.icon_unselect_gray)
                    icon_money_pay_title.isEnabled = false
                    icon_money_pay.isEnabled = false
                }
                isAccountPay -> { //余额支付 两种方式之和可以支付 则都选中
                    if (SpannableUtils.isEmptyOr0(mUserOtherBalance)){
                        firstTypesPaySv.setUnClickSelect(R.drawable.icon_unselect_gray)
                        firstTypesPayTitleTv.isEnabled = false
                        firstTypesPayIconIv.isEnabled = false
                    }else{
                        firstTypesPaySv.setSelect(true)
                        firstTypesPaySv.isEnabled = false
                    }
                    item_pay_money.setSelect(true)
                    item_pay_money.isEnabled = false

                }
                AllUtils.moneyCompare(AllUtils.priceSum_2(mUserOtherBalance,mUserAccountBalance),mOrderPayMoney)->{//两种方式之和可以支付 则都选中
                    firstTypesPaySv.setSelect(true)
                    firstTypesPaySv.isEnabled = false
                    item_pay_money.setSelect(true)
                    item_pay_money.isEnabled = false
                }
                else -> { //都不能支付
                    firstTypesPaySv.setUnClickSelect(R.drawable.icon_unselect_gray)
                    firstTypesPayTitleTv.isEnabled = false
                    firstTypesPayIconIv.isEnabled = false

                    item_pay_money.setUnClickSelect(R.drawable.icon_unselect_gray)
                    icon_money_pay_title.isEnabled = false
                    icon_money_pay.isEnabled = false
                    val drawable = pay.background as GradientDrawable
                    drawable.setColor(ResUtil.getColor(R.color.color_orange_80))
                    pay.isEnabled = false
                    showErrorTip("注：账户金额不足，请联系好微生客服进行充值。")
                }
            }
        }
    }

    private fun updateData() {
        pay_money.text = "¥$mOrderPayMoney"
        useing_remain.text = "(剩余¥$mUserAccountBalance)"
        firstTypesPayRemainTv.text = "(剩余¥$mUserOtherBalance)"
    }

    private fun showErrorTip(tip: String) {
        errorTipTv.text = tip
        errorTipTv.visibility = View.VISIBLE
    }
}